package com.yssoft.controller.app.inventory;

import com.yssoft.entity.production.TLabels;
import com.yssoft.service.inventory.InstockService;
import com.yssoft.utils.AppUtil;
import com.yssoft.utils.ObjectConversion;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @创建人: 陈翼东
 * @时间: 2018/5/16 下午 08:02
 * @说明: 入库单控制器
 */
@CrossOrigin
@RestController
@RequestMapping("/app/instockOrder")
public class InstockController {


    @Resource
    private InstockService instockService;
    
    /**
    * @方法名: getWarehousingApplicationList
    * @创建人: 陈翼东
    * @时间: 2018/5/17 上午 08:50
    * @参数: 当前页数,每页数据个数,商品类型,申请时间
    * @返回值: java.lang.Object
    * @说明: 查询当前企业的入库申请单
    */
    @RequestMapping(value = "getWarehousingApplicationList",method = RequestMethod.POST)
    public Object getWarehousingApplicationList(int page , int pageSize, String materialType, String applyDate, HttpServletRequest request){
        try {
            if(materialType == null){
                return AppUtil.returnListJson("0","请传递物料类型!",null);
            }
            if(applyDate == null){
                return AppUtil.returnListJson("0","请传递申请日期!",null);
            }
            List<Map<String,Object>> warehousingApplyList=instockService.getWarehousingApply(page,pageSize,materialType,applyDate,request);//查询物料的信息
            return AppUtil.returnListJson("1","查询成功!",warehousingApplyList);
        } catch (Exception e) {
            e.printStackTrace();
            return AppUtil.returnListJson("0","查询失败!",null);
        }
    }
    /**
    * @方法名: getWarehousingList
    * @创建人: 陈翼东
    * @时间: 2018/5/17 上午 11:49
    * @参数: [page, pageSize, materialType, applyDate, request]
    * @返回值: java.lang.Object
    * @说明: 获取入库单列表
    */
    @RequestMapping(value = "getWarehousingList",method = RequestMethod.POST)
    public Object getWarehousingList(int page , int pageSize, String materialType, String applyDate, HttpServletRequest request){
        try {
            if(materialType == null){
                return AppUtil.returnListJson("0","请传递物料类型!",null);
            }
            if(applyDate == null){
                return AppUtil.returnListJson("0","请传递申请日期!",null);
            }
            List<Map<String,Object>> warehousingList= instockService.getWarehousingList(page,pageSize,materialType,applyDate,request);
            return AppUtil.returnListJson("1","查询成功",warehousingList);
        } catch (Exception e) {
            return AppUtil.returnListJson("0","查询失败",null);
        }
    }
    
    /**
    * @方法名: getWarehousingApplication
    * @创建人: 陈翼东
    * @时间: 2018/5/17 下午 01:37
    * @参数: [warehousingApplicationId] 入库申请单id
    * @返回值: java.lang.Object
    * @说明: 根据入库申请单查询入库申请明细id
    */
    @RequestMapping(value = "getWarehousingApplication",method = RequestMethod.POST)
    public Object getWarehousingApplication(String warehousingApplicationId,String warehousingApplicationDetailId){
        try {
            if(warehousingApplicationId == null){
                return AppUtil.returnListJson("0","请传递申请单id!",null);
            }
            if(warehousingApplicationDetailId == null){
                return AppUtil.returnListJson("0","请传递申请详情id!",null);
            }
            Map<String,Object> wareApplyDetail=instockService.getWarehousingApplyDetail(warehousingApplicationId,warehousingApplicationDetailId);
            return AppUtil.returnMapJson("1","查询成功",wareApplyDetail);
        } catch (Exception e) {
            e.printStackTrace();
            return AppUtil.returnListJson("0","查询失败",null);
        }
    }
    
    /**
    * @方法名: saveWarehousingMaterial
    * @创建人: 陈翼东
    * @时间: 2018/5/17 下午 01:43
    * @参数: [warehousingApplicationId, warehousingDetailNumber] 入库申请单id,入库详情id和数量的合并(使用&链接)页面标签name使用这个详情id
    * @返回值: java.lang.Object
    * @说明: 保存物料，添加剂,辅料,包材的入库信息
    */
    @RequestMapping(value = "saveWarehousingMaterial",method = RequestMethod.POST)
    public Object saveWarehousingMaterial(String warehousingApplicationId,String[] warehousingApplicationDetailId,String[] warehousingDetailNumber,HttpServletRequest request){
        if(warehousingApplicationId==null){
            return AppUtil.returnMapJson("0","请传递入库申请单id",null);
        }
        return instockService.saveWarehousingMaterial(warehousingApplicationId,warehousingApplicationDetailId,warehousingDetailNumber,request);
    }
    
    /**
    * @方法名: getWorksheetByBatch
    * @创建人: 陈翼东
    * @时间: 2018/5/18 下午 04:16
    * @参数: []
    * @返回值: java.lang.Object
    * @说明: 根据标签码查询加工单
    */
    @RequestMapping(value = "getWorksheetByBatch",method = RequestMethod.POST)
    public Object getWorksheetByBatch(String code){
        try {
            Map<String,Object> worksheet=instockService.getApplyDetailByBatch(code);
            return AppUtil.returnMapJson("1","查询成功!",worksheet);
        } catch (Exception e) {
            e.printStackTrace();
            return AppUtil.returnMapJson("0","查询失败!",null);
        }
    }
    
    /**
    * @方法名: getWorksheetByApplyDetail
    * @创建人: 陈翼东
    * @时间: 2018/5/18 下午 04:47
    * @参数: [applyDetailId]
    * @返回值: java.lang.Object
    * @说明: 根据申请明细查询加工单
    */
    @RequestMapping(value = "getWorksheetByApplyDetail",method = RequestMethod.POST)
    public Object getWorksheetByApplyDetail(String applyDetailId){
        try {
            Map<String,Object> worksheet=instockService.getWorksheetByApplyDetail(applyDetailId);
            return AppUtil.returnMapJson("1","查询成功!",worksheet);
        } catch (Exception e) {
            e.printStackTrace();
            return AppUtil.returnMapJson("0","查询失败!",null);
        }
    }
    
    /**
     * @方法名: getWorksheetByApplyDetail
     * @创建人: 陈翼东
     * @时间: 2018/5/18 下午 04:47
     * @参数: [worksheetid]
     * @返回值: java.lang.Object
     * @说明: 根据加工单id获取加工单信息和批次信息
     */
    @RequestMapping(value = "getBatchByWorkshetId",method = RequestMethod.POST)
    public Object getBatchByWorkshetId(String worksheetid){
        try {
            Map<String,Object> worksheetAndBatch=instockService.getWorksheetAndPeatchByBeatch(worksheetid);
            return AppUtil.returnMapJson("1","查询成功!",worksheetAndBatch);
        } catch (Exception e) {
            e.printStackTrace();
            return AppUtil.returnMapJson("0","查询失败!",null);
        }
    }
    
    /**
    * @方法名: getLableByCode
    * @创建人: 陈翼东
    * @时间: 2018/5/18 下午 08:16
    * @参数: [code]
    * @返回值: java.lang.Object
    * @说明: 根据标签码获取标签
    */
    @RequestMapping(value = "getLableByCode",method = RequestMethod.POST)
    public Object getLableByCode(String code){
        try {
            TLabels label = instockService.getLabelByCode(code);
            return  AppUtil.returnMapJson("1","获取成功!", ObjectConversion.getObjectToMap(label));
        } catch (Exception e) {
            e.printStackTrace();
            return  AppUtil.returnMapJson("0","获取失败!",null);
        }
    }
    
    /**
    * @方法名: saveWarehousingBatch
    * @创建人: 陈翼东
    * @时间: 2018/5/18 下午 08:06
    * @参数: []
    * @返回值: java.lang.Object
    * @说明:
    */
    @RequestMapping(value = "saveWarehousingBatch",method = RequestMethod.POST)
    public Object saveWarehousingBatch(String warehousingApplicationId,String[] warehousingApplicationDetailId,String[] warehousingDetailNumber,String[] labelId,HttpServletRequest request){
        return instockService.saveWarehousingBatch(warehousingApplicationId,warehousingApplicationDetailId,warehousingDetailNumber,labelId,request);
    }
    
    /**
    * @方法名: getWorksheetByLabelPackage
    * @创建人: 陈翼东
    * @时间: 2018/5/19 上午 09:17
    * @参数: [code]
    * @返回值: java.lang.Object
    * @说明:
    */
    @RequestMapping(value = "getWorksheetByLabelPackage",method = RequestMethod.POST)
    public Object getWorksheetByLabelPackage(String codeId){
        try {
            Map<String,Object> worksheet= instockService.getWorksheetByLabelPackage(codeId);
            return AppUtil.returnMapJson("1","查询成功!",worksheet);
        } catch (Exception e) {
            e.printStackTrace();
            return AppUtil.returnMapJson("0","查询失败!",null);
        }
    }
    
    /**
    * @方法名: getWorksheetByPackageCode
    * @创建人: 陈翼东
    * @时间: 2018/5/19 上午 09:58
    * @参数: [packageCode]
    * @返回值: java.lang.Object
    * @说明: 根据包装码获取加工单信息
    */
    @RequestMapping(value = "getWorksheetByPackageCode" ,method = RequestMethod.POST)
    public Object getWorksheetByPackageCode(String packageCode){
        try {
            Map<String,Object> worksheet= instockService.getWorksheetByPackageCode(packageCode);
            return AppUtil.returnMapJson("1","查询成功!",worksheet);
        } catch (Exception e) {
            e.printStackTrace();
            return AppUtil.returnMapJson("0","查询失败!",null);
        }
    }
    
    /**
    * @方法名: getLablesByPackageCode
    * @创建人: 陈翼东
    * @时间: 2018/5/19 上午 09:58
    * @参数: [packageCode]
    * @返回值: java.lang.Object
    * @说明: 根据包装码获取标签信息
    */
    @RequestMapping(value = "getLablesByPackageCode" ,method = RequestMethod.POST)
    public Object getLablesByPackageCode(String packageCode){
        try {
            List<Map<String,Object>> labels= instockService.getLablesByPackageCode(packageCode);
            return AppUtil.returnListJson("1","查询成功!",labels);
        } catch (Exception e) {
            e.printStackTrace();
            return AppUtil.returnMapJson("0","查询失败!",null);
        }
    }
    
    /**
    * @方法名: saveWarehousingUnit
    * @创建人: 陈翼东
    * @时间: 2018/5/19 下午 01:18
    * @参数: [warehousingApplicationId, warehousingApplicationDetailId, warehousingDetailNumber, labelId, request]
    * @返回值: java.lang.Object
    * @说明: 个体入库保存
    */
    @RequestMapping(value ="saveWarehousingUnit",method = RequestMethod.POST)
    public Object saveWarehousingUnit(String warehousingApplicationId,String warehousingApplicationDetailId,String[] labelId,HttpServletRequest request){
        try {
            return instockService.saveWarehousingUnit(warehousingApplicationId,warehousingApplicationDetailId,labelId,request);
        } catch (Exception e) {
            e.printStackTrace();
            return AppUtil.returnMapJson("0","保存失败!",null);
        }
    }
}
